# coding:utf-8
# @Time    : 2021/11/29 17:35
# @Author  : Martin
from selenium.common.exceptions import UnexpectedAlertPresentException


from page_object import *
from page_object.base.base_page import BasePage
from page_object.carrefour.order_free_post_page import OrderFreePostPage
from page_object.carrefour.single_line_reduce_page import SingleLinePage
from page_object.carrefour.special_reduce_page import SpecialReducePage
from page_object.shop.combine_package import CombinePackagePage
from page_object.shop.offline_nonshop_coupon import OfflineNonCoupon, OfflineNonCouponCreate
from page_object.shop.online_noshop_coupon import OnlineNoShopCoupon
from page_object.shop.online_order_reduce import OnlineOrderReduceCreate
from page_object.shop.order_option_package_page import OrderOptionPackage
from page_object.shop.order_trade_page import OrderTradePage


class WelcomePage(BasePage):
    _base_url = "/nspas-web-in/welcome.htm"

    def go_to_page(self):
        return OfflineNonCoupon(self.driver, self.logger)

    def go_to_online_order_reduce(self):
        return OnlineOrderReduceCreate(self.driver, self.logger, '1')

    def go_to_offline_order_reduce(self):
        return OfflineOrderReduceCreate(self.driver, self.logger, '1')

    def go_to_come_gift(self):
        return OfflineComeGift(self.driver, self.logger)

    def go_to_offline_order_gift(self):
        return OrderGiftPage(self.driver, self.logger, '1')

    def go_to_online_order_gift(self):
        return OnlineOrderGiftPage(self.driver, self.logger, '1')

    def go_to_online_floor_gift(self):
        return OnlineFloorGiftPage(self.driver, self.logger)

    def go_to_query_page(self):
        return QueryPage(self.driver, self.logger)

    def go_to_free_combine(self) -> FreeCombinePackage:
        return FreeCombinePackage(self.driver, self.logger)

    def go_to_order_trade(self) -> OrderTradePage:
        return OrderTradePage(self.driver, self.logger, '2', '1')

    def go_to_online_combine_package(self) -> CombinePackagePage:
        return CombinePackagePage(self.driver, self.logger, '2', '1')

    def go_to_online_option_package(self):
        return OrderOptionPackage(self.driver, self.logger)

    def go_to_online_noshop_coupon(self):
        return OnlineNoShopCoupon(self.driver, self.logger)

    def go_to_offline_noshop_coupon(self):
        return OfflineNonCouponCreate(self.driver, self.logger, 'NSOA23362')

    def go_to_special_reduce(self):
        return SpecialReducePage(self.driver, self.logger)

    def go_to_single_line_reduce(self):
        return SingleLinePage(self.driver, self.logger)

    def go_to_order_free_post(self):
        return OrderFreePostPage(self.driver, self.logger)

    def log_out(self):
        from page_object.login_page import LoginPage
        # try:
        #     current_url = self.driver.current_url
        # except UnexpectedAlertPresentException:
        #     self.switch_alert()
        #     current_url = self.driver.current_url
        try:
            if self.driver.current_url != self._base_url:
                self.open(self._base_url)
        except UnexpectedAlertPresentException:
            self.switch_alert()
            if self.driver.current_url != self._base_url:
                self.open(self._base_url)
        self.click('id', 'logout')
        # try:
        #     self.click('id', 'logout')
        # except UnexpectedAlertPresentException:
        #     self.switch_alert()
        #     self.click('id', 'logout')

        return LoginPage(self.driver, self.logger)

    def login_user(self):
        """
        获取当前登录用户
        """
        name = self.get_element_txt('id', 'frame_userName')
        return name
